package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    private R<String>save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        String s = "dish_" + dishDto.getCategoryId();
        redisTemplate.delete(s);
        return R.success("新增菜品成功");
    }
    @GetMapping("/page")
    public R<Page<DishDto>>page(Integer page,Integer pageSize,String name){
//        Page<Dish> pageInfo1 = new Page<>(page,pageSize);
//        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
//        queryWrapper.orderByAsc("update_time");
//        dishService.page(pageInfo1,queryWrapper);
//        Page<DishDto> pageInfo2 = new Page<>(page,pageSize);
//        ArrayList<DishDto> dishDtoList = new ArrayList<>();
//        for (Dish dish:pageInfo1.getRecords()){
//            DishDto dishDto = new DishDto();
//            BeanUtils.copyProperties(dish,dishDto);
//            dishDtoList.add(dishDto);
//        }
//        pageInfo2.setRecords(dishDtoList);
//        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
//        return R.success(pageInfo2);
        Page<DishDto> dtoPage = dishService.selectPageByName(page, pageSize, name);
        return R.success(dtoPage);
    }
    @GetMapping("/{id}")
    public R<DishDto>getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        String key="dish_"+ dishDto.getCategoryId();
        redisTemplate.delete(key);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String>update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        String key="dish_"+ dishDto.getCategoryId();
        redisTemplate.delete(key);
        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>>list(Dish dish){
        String key="dish_"+ dish.getCategoryId();
        List<DishDto> dishDtos = redisTemplate.opsForList().range(key,0,-1);
        if (dishDtos == null || dishDtos.size() ==0){
            dishDtos= dishService.listWithFlavors(dish);
            redisTemplate.opsForList().leftPushAll(key,dishDtos);
        }
        redisTemplate.opsForValue().set(key,dishDtos,5, TimeUnit.MILLISECONDS);
        return R.success(dishDtos);
    }

    @DeleteMapping
    public R<String> delete(Long[] ids){
        dishService.removeWithDish(ids);
        return R.success("删除数据成功");
    }
}
